﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace IDCardNumberInterpreter
{
    /// <summary>
    /// id 的摘要说明
    /// </summary>
    public class id : IHttpHandler
    {
        private HttpRequest mRequest = HttpContext.Current.Request;
        private HttpResponse mResponse = HttpContext.Current.Response;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                StringBuilder sb = new StringBuilder();

                string number = mRequest.QueryString["number"];

                sb.AppendLine("身份证号：" + number);

                if (number.Length != 18)
                {
                    sb.AppendLine("请输入18位身份证号");
                    mResponse.Write(sb.ToHtml());
                    return;
                }

                //省 12            //市 34            //区 56
                sb.AppendLine("地区：" + DataUtil.GetZone(number.Substring(0, 6)));

                //镇
                //村

                //生日 78901234
                sb.AppendLine("生日：" + DataUtil.GetDateTime(number.Substring(6, 8)).ToString("yyyy年MM月dd日"));

                //派出所 56
                sb.AppendLine("派出所编号：" + number.Substring(14, 2));

                //序号 7
                sb.AppendLine("序号：" + number.Substring(16, 1));

                //性别 7
                sb.AppendLine("性别：" + DataUtil.GetSex(number.Substring(16, 1)));

                //校验 8
                sb.AppendLine("输入校验码：" + number.Substring(17, 1));

                //校验结果
                sb.AppendLine("计算校验码：" + DataUtil.GetVerify(number));

                mResponse.Write(sb.ToHtml());
            }
            catch (Exception ex)
            {
                mResponse.Write(ex.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}